package com.cskaoyan.demo6.service;

import com.cskaoyan.demo6.bean.model.MarketAd;
import com.cskaoyan.demo6.bean.model.MarketAdExample;
import com.cskaoyan.demo6.bean.vo.HomeIndexVo;
import com.cskaoyan.demo6.mapper.MarketAdMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/20 22:33
 */

@Service("adService")
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public List<HomeIndexVo.BannerDTO> select(Integer offset, Integer bannerLimit) {
        MarketAdExample adExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        PageHelper.offsetPage(0, bannerLimit);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(adExample);
        List<HomeIndexVo.BannerDTO> banner = new ArrayList<>();
        marketAds.forEach(marketAd -> {
            HomeIndexVo.BannerDTO bannerDTO = new HomeIndexVo.BannerDTO();
            BeanUtils.copyProperties(marketAd, bannerDTO);
            banner.add(bannerDTO);
        });
        return banner;
    }

    @Override
    public List<HomeIndexVo.BannerDTO> query() {
        MarketAdExample adExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = adExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(adExample);
        List<HomeIndexVo.BannerDTO> banner = new ArrayList<>();
        marketAds.forEach(marketAd -> {
            HomeIndexVo.BannerDTO bannerDTO = new HomeIndexVo.BannerDTO();
            BeanUtils.copyProperties(marketAd, bannerDTO);
            banner.add(bannerDTO);
        });
        return banner;
    }
}
